
$(function(){
	$(".favor_cart").hover(function(){
		$(this).addClass("favor_cart_hover"); 
	},function(){
		$(this).removeClass("favor_cart_hover");
	});
 
	$('#BeiJing').show();
		$('#cart_title').hide();
		$('#DaoHang').hide();
		$('#jiesuan').hide();
 
	$("#shopping").hover(function(){
		$("#favor_dv").hide();
 
		$("#shopping_dv").show();
		$("#yuandian1").addClass("right_style1");
		$("#yuandian2").removeClass("right_style1");
		$("#yuandian2").addClass("right_style");
 
	},null);
 
	$("#guess").hover(function(){
		$("#shopping_dv").hide();
		$("#favor_dv").show();
		$("#yuandian2").addClass("right_style1");
		$("#yuandian1").removeClass("right_style1");
	},null);
 
	$("#header>ul>li").mouseover(function(){
		$(this).addClass("iconfont1_hover");
		$(this).children('ul').show();
	});
	$("#header>ul>li").mouseout(function(){
		$(this).removeClass("iconfont1_hover");
		$(this).children('ul').hide();
	});
 
	$(".item_dv").hover(function(){
		$(this).addClass("item_dvTF"); 
	},function(){
		$(this).removeClass("item_dvTF");
	});
 
});
var Itemnumber=0;
var SPNB=0;
var ZJ=0;
//点击按钮增加数量
function increment(goodsId){
 
	var new_num=parseInt($("#number"+goodsId).val())+1;
	// alert(goodsId);
	$("#number"+goodsId).val(new_num);
	itemPrice(goodsId);
}
//点击按钮减少数量
function crement(goodsId){
 
	var new_num=parseInt($("#number"+goodsId).val())-1;
	if(new_num>=1){
	$("#number"+goodsId).val(new_num);
	itemPrice(goodsId);
	}
	
}
function selectremove(){
	for(i=1;i<9;i++){
		
	if($('#SELE'+i).is(':checked')){
			$("#cart_item"+i).remove();
			Itemnumber=Itemnumber-1;
	//alert(Itemnumber);
	if(Itemnumber==0){
 
		$('#BeiJing').show();
		$("#SPnumber").text("已选择0件商品");
		$("#zongjia").text("￥0");
		$('#cart_title').hide();
		$('#DaoHang').hide();
		$('#jiesuan').hide();
	}
 
		}
	}
	$("#SPnumber").text("已选择0件商品");
	$("#zongjia").text("￥0");
}
// 计算小计的内容
function itemPrice(goodsId){
 
	var shuliang=parseInt($("#number"+goodsId).val());
	var jia=$("#item_danjia"+goodsId).text().replace("￥","");
	
	var xiaoji=jia*shuliang;
 
	$("#item_xiaoji"+goodsId).text("￥"+xiaoji.toFixed(2));
}
// 删除一条商品
function removeItem(goodsId){
	$("#cart_item"+goodsId).remove();
	Itemnumber=Itemnumber-1;
	//alert(Itemnumber);
	// alert(Itemnumber);
	if(Itemnumber==0){
 
		$('#BeiJing').show();
		$("#SPnumber").text("已选择0件商品");
		$("#zongjia").text("￥0");
		$('#cart_title').hide();
		$('#DaoHang').hide();
		$('#jiesuan').hide();
	}
}
 
//全选
function allSelect(){
	// alert("111");
	$("input[type='checkbox']").prop("checked","true");
	
}
//全删 
function allremove(){
	for(i=1;i<9;i++){
		$("#cart_item"+i).remove();
	}
	$('#BeiJing').show();
	$("#SPnumber").text("已选择0件商品");
	$("#zongjia").text("￥0");
		$('#cart_title').hide();
		$('#DaoHang').hide();
		$('#jiesuan').hide();
}
//添加一条商品到购物车
 
function addItem(goodsId){
 
	//判断是否有这个商品
	var list = $("#test1").find("#cart_item"+goodsId);
		if(list.length > 0){
			increment(goodsId);
 
 
		}
		else{
 
	$('#cart_title').show();
	$('#DaoHang').show();
	$('#jiesuan').show();
	$('#BeiJing').hide();
	var item_dv_id="#item_dv"+goodsId;
	var cart_item_id="cart_item"+goodsId;
	var title=$(item_dv_id+" .title").text();
	Itemnumber=Itemnumber+1;
	//alert(Itemnumber);
	
	var img_url=$(".favor_image"+goodsId).css("background-image");
 
	img_url = img_url.split("(")[1].replace(")","");
	 jiage=$(item_dv_id+" .price").text();
	
	// alert(jiage);
	
 
	var content="<div class='cart_item' id='"+cart_item_id+"'>"
			+"<div class='cart_item_select'>"
			   +"<div class='select_cd'><input id='SELE"+goodsId+"' type='checkbox' ></div>"
			   +"<div class='cart_item_image'><img class='item_image' src="+img_url+"> </div>"
 
			+"</div>"
		+"<div class='item_shangpin'>"
		+title
		+"</div>"
		+"<div class='item_danjia' id='item_danjia"+goodsId+"'>"
			+jiage
			+"</div>"
			+"<div class='item_shuliang'>"
				+"<input class='crement' type='button' value='-' onclick='crement("+goodsId+");'><input class='number' id='number"+goodsId+"' type='text' value='1' disabled='true'><input class='increment' type='button' value='+' onclick='increment("+goodsId+");'>"
			+"</div>"
			+"<div class='item_xiaoji' id='item_xiaoji"+goodsId+"'>"
			+jiage
			+"</div>"
			+"<div class='caozuo'>"
			+"<a href='javascript:removeItem("+goodsId+");'>删除</a>"	
			+"</div>"
		+"</div>";
		$("#test1").append(content);
	}
}
 
//结算
function Alljs(){
 
	for(i=1;i<9;i++){
		var test5=parseInt($("#number"+i).val());
	if($('#SELE'+i).is(':checked')&&test5>1){
		SPNB=SPNB+parseInt($("#number"+i).val());
		SPNB=SPNB-1;
		
		}
	
	}
	
	if($('#box').is(':checked')) {
		
	
 	var shuliang=$("input[type='checkbox']:checked").length-1+SPNB; 
 	
}else{
	
	var shuliang=$("input[type='checkbox']:checked").length+SPNB;
	
}
	
	$("#SPnumber").text("已选择"+shuliang+"件商品");
	for(i=1;i<9;i++){
 
	if($('#SELE'+i).is(':checked')){
		var test4=$("#item_xiaoji"+i).text();
		test4 = test4.split("￥")[1];
		
		ZJ=ZJ+parseFloat(test4);
		
		$("#zongjia").text("总价：￥"+ZJ.toFixed(2));
 
	}
	}
	ZJ=0;
	SPNB=0;
	
}
 
 
//拖拽
 
function allowDrop(ev)
{
ev.preventDefault();
}
 
function drag(ev)
{
ev.dataTransfer.setData("asdf",1);	
 
}
 
function drag1(ev)
{
ev.dataTransfer.setData("asdf",2);
 
}
 
function drag2(ev)
{
ev.dataTransfer.setData("asdf",3);
 
}
 
function drag3(ev)
{
ev.dataTransfer.setData("asdf",4);
 
}
 
function drag4(ev)
{
ev.dataTransfer.setData("asdf",5);
 
}
 
function drag5(ev)
{
ev.dataTransfer.setData("asdf",6);
 
}
 
function drag6(ev)
{
ev.dataTransfer.setData("asdf",7);
 
}
 
function drag7(ev)
{
ev.dataTransfer.setData("asdf",8);
 
}
 
function addItem1(ev){
 
ev.preventDefault();
var data=ev.dataTransfer.getData("asdf");
	//判断是否有这个商品
	var list = $("#test1").find("#cart_item"+data);
		if(list.length > 0){
			increment(data);
 
		}
		else{
 
	$('#cart_title').show();
	$('#DaoHang').show();
	$('#jiesuan').show();
	$('#BeiJing').hide();
	var item_dv_id="#item_dv"+data;
	var cart_item_id="cart_item"+data;
	var title=$(item_dv_id+" .title").text();
	Itemnumber=Itemnumber+1;
	//alert(Itemnumber);
	
	var img_url=$(".favor_image"+data).css("background-image");
 
	img_url = img_url.split("(")[1].replace(")","");
	 jiage=$(item_dv_id+" .price").text();
	
	
 
	var content="<div class='cart_item' id='"+cart_item_id+"'>"
			+"<div class='cart_item_select'>"
			   +"<div class='select_cd'><input id='SELE"+data+"' type='checkbox' ></div>"
			   +"<div class='cart_item_image'><img class='item_image' src="+img_url+"> </div>"
 
			+"</div>"
		+"<div class='item_shangpin'>"
		+title
		+"</div>"
		+"<div class='item_danjia' id='item_danjia"+data+"'>"
			+jiage
			+"</div>"
			+"<div class='item_shuliang'>"
				+"<input class='crement' type='button' value='-' onclick='crement("+data+");'><input class='number' id='number"+data+"' type='text' value='1' disabled='true'><input class='increment' type='button' value='+' onclick='increment("+data+");'>"
			+"</div>"
			+"<div class='item_xiaoji' id='item_xiaoji"+data+"'>"
			+jiage
			+"</div>"
			+"<div class='caozuo'>"
			+"<a href='javascript:removeItem("+data+");'>删除</a>"	
			+"</div>"
		+"</div>";
		$("#test1").append(content);
 
		}
}